package org.mpremote.android.framework.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import org.mpremote.android.R;
import org.mpremote.android.framework.xml.Album;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Gilad
 * Date: 08/10/2010
 * Time: 19:06:43
 */
public class AlbumsListAdapter extends ArrayAdapter<Album> {

    private int resourceId = 0;
    private LayoutInflater inflater;
    private Context context;

    public AlbumsListAdapter(Context context, int textViewResourceId, List<Album> objects) {
        super(context, 0, objects);
        this.resourceId = textViewResourceId;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final TextView artistName;
        final ImageView artistImage;
        View view = inflater.inflate(resourceId, parent, false);
        artistName = (TextView) view.findViewById(R.id.album_name);
        artistImage = (ImageView) view.findViewById(R.id.album_artwork);

        Album item = getItem(position);
        Bitmap cachedImage = null;
//        try {
//            cachedImage = imageLoader.loadImage(item.thumbnail, new ImageLoadedListener() {
//                public void imageLoaded(Bitmap imageBitmap) {
//                    image.setImageBitmap(imageBitmap);
//                    notifyDataSetChanged();
//                }
//            });
//        } catch (MalformedURLException e) {
//            Log.e(TAG, "Bad remote image URL: " + item.thumbnail, e);
//        }

        artistName.setText(item.getName());
        artistImage.setImageResource(R.drawable.missing_picture);

        if (cachedImage != null) {
            artistImage.setImageBitmap(cachedImage);
        }

        return view;
    }
}
